package cn.com.java.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.com.java.core.HttpRes;
import cn.com.java.model.Order;
import cn.com.java.model.Product;
import cn.com.java.model.ProductImages;
import cn.com.java.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.micrometer.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("order")
@RequiredArgsConstructor
public class OrderController {

    
    private final OrderService orderService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public List<Order> listAll() {
        return orderService.list();
    }

  /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
  @PostMapping("listByPage")
  public HttpRes listByPage(@RequestBody Order order) {
      Page<Order> page = new Page<>(order.getPageIndex(), order.getPageSize());
      QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
      if(StringUtils.isNotBlank(order.getStatus())){
          orderQueryWrapper.eq("status", order.getStatus());
      }
      if(StringUtils.isNotBlank(order.getType())){
          orderQueryWrapper.eq("type", order.getType());
      }
      return HttpRes.OK("查询成功", orderService.page(page, orderQueryWrapper));
  }


    /**
     *
     * @param userId
     * @return
     */
    @RequestMapping("getByUserId")
    public HttpRes getByUserId(Long userId) {
        return HttpRes.OK(orderService.getByUserId(userId));
    }



    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回记录，没有返回null
     */
    @RequestMapping("getById")
    public Order getById(Long id) {
        return orderService.getById(id);
    }

     
    /**
     * 新增，忽略null字段
     *
     * @param order 新增的记录
     * @return 返回影响行数
     */
    @RequestMapping("insert")
    public boolean insert(@RequestBody Order order) {
        return orderService.save(order);
    }    
      
    /**
     * 修改，忽略null字段
     *
     * @param order 修改的记录
     * @return 返回影响行数
     */
    @RequestMapping("update")
    public boolean update(@RequestBody Order order) {
        return orderService.updateById(order);
    }
    
    /**
     * 删除记录
     *
     * @param order 待删除的记录
     * @return 返回影响行数
     */
    @RequestMapping("delete")
    public HttpRes delete(Long id) {
        return HttpRes.OK(orderService.removeById(id));
    }


    @RequestMapping("buyOrder")
    public HttpRes buyOrder(Long productId, Long type, Long userId, Integer num) {
        return HttpRes.OK(orderService.buyOrder(productId, type,userId,num) );
    }
}